package com.ruoyi.framework.advice;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.IdToString;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.annotation.Annotation;
import java.util.List;

/**
 * @author bwzfy
 * @create 2023/3/5
 **/
@ControllerAdvice
public class ResponseBodyAnalysis implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        // TODO: 未修改完成
//        System.out.println("获取了   " + body);
//        if (body instanceof TableDataInfo) {
//            TableDataInfo tableDataInfo = (TableDataInfo) body;
//            List list = tableDataInfo.getRows();
//            for (int i = 0; i < list.size(); i++) {
//                Object o = list.get(0);
//            }
//            System.out.println(list);
//        }else if (body instanceof AjaxResult) {
//
//        }
        return body;
    }

    public boolean hasAnnotation(Class clazz, Annotation annotation) {
        return clazz.getAnnotation(IdToString.class) != null;
    }
}
